package com.hdoit.ares.web.ser.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.ser.model.Staff;
import com.hdoit.ares.logical.ser.service.IGoodStoreService;
import com.hdoit.ares.logical.ser.service.IStaffService;
import com.hdoit.ares.logical.viewmodel.ser.StaffSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.logical.superbase.service.IRoleEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      lvsi
 * @email       
 * @createtime  2017年11月27日 11:26:26
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/ser/staff/")
public class StaffController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/ser/staff/";
    }

    private IStaffService staffService;
    public IStaffService getStaffService() {
        return staffService;
    }
    @Resource
    public void setStaffService(IStaffService staffService) {
        this.staffService = staffService;
    }

    private IRoleEmployeeService roleEmployeeService;	
	public IRoleEmployeeService getRoleEmployeeService() {
		return roleEmployeeService;
	}	
	@Resource
	public void setRoleEmployeeService(IRoleEmployeeService roleEmployeeService) {
		this.roleEmployeeService = roleEmployeeService;
	}
	private IGoodStoreService goodStoreService;
    public IGoodStoreService getGoodStoreService() {
        return goodStoreService;
    }
    @Resource
    public void setGoodStoreService(IGoodStoreService goodStoreService) {
        this.goodStoreService = goodStoreService;
    }
	
    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="staffDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,StaffSearch staffSearch,boolean ajaxPost){
    	String loginId=getLoginModel().getLoginId();
    	boolean result=roleEmployeeService.queryCountRolesByEmployeeId("超级管理员",loginId);
    	List<Staff> staffs=null;
    	if(goodStoreService.queryListStoreById(loginId).size()==0 && staffService.queryStaffById(loginId).size()==0
    			&& result==true){
    		pagerModel.setTotalAmount(staffService.queryAllStaffCount(staffSearch, pagerModel));
            staffs= staffService.queryAllStaff(staffSearch,pagerModel);
    	}else{
    		pagerModel.setTotalAmount(staffService.queryStaffCountById(staffSearch,loginId));
            staffs= staffService.queryStaffListById(staffSearch,pagerModel, loginId);
    	}
        

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("staffs", staffs);
        model.addAttribute("staffSearch", staffSearch);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String staffDetailed(Model model,String key){
        Staff staff=new Staff();
        staff.setJoinUsDate(DateUtils.getCurrent());
        if(!StringUtils.isEmpty(key)){
            staff=staffService.queryModel(key);
            List<String> roleIds = roleEmployeeService.queryRoleIdsByEmployeeId(key);
            model.addAttribute("roleIds",roleIds);
        }
        model.addAttribute("staff", staff);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Staff staff){
        if(StringUtils.isEmpty(staff.getEmployeeId())){
            staff.setEmployeeId(CommonUtils.getKeyId());
            staff.setCreateDate(DateUtils.getCurrent());
            staff.setDeleteMark(YesOrNo.no);
            staff.setCreateUserId(getLoginModel().getLoginId());
            staff.setCreateUserName(getLoginModel().getLoginName());
            staff.setModelStatus(ModelStatus.add);
        }else{
            staff.setModifyDate(DateUtils.getCurrent());
            staff.setModifyUserId(getLoginModel().getLoginId());
            staff.setModifyUserName(getLoginModel().getLoginName());
            staff.setModelStatus(ModelStatus.update);
        }
        
        staff.setMarkCode(ChineseUtils.getPinYinHeadChar(staff.getEmployeeName()));

        ActionResult actionResult;
        if(staffService.applyChange(staff)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("staff", staff);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(staffService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}